package com.blog.boot.webapp.controller.admin;

import com.blog.boot.api.common.RestMsg;
import com.blog.boot.api.entity.UserEntity;
import com.blog.boot.api.system.service.SystemService;
import com.blog.boot.webapp.controller.common.BaseController;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Autowired
    SystemService systemService;
    @RequestMapping("/token")
    @ResponseBody
    public RestMsg token(String token){
        RestMsg msg = restFactory.getMsg();
        msg.setMsg("验证成功");
        return msg;
    }

    /**
     * 登录状态
     */
    @RequestMapping("/session")
    @ResponseBody
    public RestMsg session(){
        RestMsg msg = restFactory.getMsg();
        Session session = SecurityUtils.getSubject().getSession();
        UserEntity user =(UserEntity) session.getAttribute("user");
        return Objects.nonNull(user) ? msg.setData(user).setMsg("验证成功") : msg.setData(null).setMsg("请登录");
    }

    /**
     * 查询角色
     */
    @RequestMapping("/user/role")
    @ResponseBody
    public RestMsg role(){
        RestMsg msg = restFactory.getMsg();
        return msg.setData(systemService.getRole());
    }

    /**
     * 查询权限
     */
    @RequestMapping("/user/permission")
    @ResponseBody
    public RestMsg permission(){
        RestMsg msg = restFactory.getMsg();
        return msg.setData(systemService.getPermission());
    }
}
